package com.example.littleken.secret.Activitys;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.littleken.secret.Net.Message;
import com.example.littleken.secret.R;

import java.util.ArrayList;
import java.util.List;

/**本类为主页面信息显示的适配器
 * Created by littleken on 2018/4/25.
 */

public class AtyTimelineMessageListAdapter extends BaseAdapter {
    private Context context = null;
    private List<Message> data = new ArrayList<Message>();

    public AtyTimelineMessageListAdapter(Context context) {
        this.context = context;
    }

    public void addAll(List<Message> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }
    public void clear(){
        data.clear();
        notifyDataSetChanged();
    }

    public Context getContext() {
        return context;
    }

    @Override
    public int getCount() {
        return (data != null)?data.size():0;
    }

    @Override
    public Message getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //TextView tvcelllable;
        ListCell lc;
        Message msg = getItem(position);
       if(convertView == null){//一级优化
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.aty_timeline_list_cell,null);
            convertView.setTag(new ListCell((TextView) convertView.findViewById(R.id.tvCellLabel)));//二级优化
        }

        lc = (ListCell) convertView.getTag();
       // tvcelllable = (TextView) convertView.findViewById(R.id.tvCellLabel);
        msg = getItem(position);
        lc.getTvCellLable().setText(msg.getMsg());
        return convertView;
    }

    public static class ListCell{
        private TextView tvCellLable;

        public ListCell(TextView tvCellLable) {
            this.tvCellLable = tvCellLable;
        }

        public TextView getTvCellLable() {
            return tvCellLable;
        }
    }
}
